/* 
 *  Copyright 2009-2010 Michal Trzcinka
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software distributed
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 */

YAHOO.namespace('TRZCINKA.VCARD');

(function() {

  var LOGGER = new YAHOO.widget.LogWriter('cells.js'),
  Config = YAHOO.TRZCINKA.VCARD.Config;

  /**
   * Cells
   */

  YAHOO.TRZCINKA.VCARD.Cells = function(gridModel, parent, rows, columns) {
    this._cells = [];
    this._gridModel = gridModel;
    this._table = document.createElement('table');

    this._table.id = Constants.TABLE_ID;
    Dom.addClass(this._table, 'grid');
    Dom.setStyle(this._table, 'width', (Config.CELL_WIDTH * columns) + 'px');
    Dom.setStyle(this._table, 'height', (Config.CELL_HEIGHT * rows) + 'px');

    for (var y = 0; y < rows; y++) {
      var row = document.createElement('tr');
      for (var x = 0; x < columns; x++) {
        var cell = new YAHOO.TRZCINKA.VCARD.Cell({
          row: y,
          column: x
        }, gridModel);
        row.appendChild(cell.htmlElement);
        this.putCell(cell);
      }
      Dom.setStyle(row, 'height', Config.CELL_HEIGHT + 'px');
      this._table.appendChild(row);
    }

    parent.appendChild(this._table);

    if (gridModel.accountType.canEditGrid()) {
      YAHOO.TRZCINKA.VCARD.buildContextMenu(this._table.id, gridModel);
    }

    this._rows = rows;
    this._columns = columns;
  }
  
  YAHOO.TRZCINKA.VCARD.Cells.prototype.getCell = function(location) {
    return this._cells.find(function(cell) {
      return cell.location.equals(location);
    });
  };

  YAHOO.TRZCINKA.VCARD.Cells.prototype.putCell = function(cell) {
    this._cells.push(cell);
  };

})();
